package com.yzqp.common.util;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class TimeUtil {

    /**
     * 格式化时间
     *
     * @param date
     * @return 日期时间字符串
     */
    public static String formtDateTime(Date date) {
        if (date == null) {
            return null;
        }
        String strDateFormat = "yyyy-MM-dd HH:mm:ss";
        SimpleDateFormat sdf = new SimpleDateFormat(strDateFormat);
        return sdf.format(date);
    }

    /**
     * 格式化使劲
     *
     * @param date
     * @return 日期字符串
     */
    public static String formtDate(Date date) {
        if (date == null) {
            return null;
        }
        String strDateFormat = "yyyy-MM-dd";
        SimpleDateFormat sdf = new SimpleDateFormat(strDateFormat);
        return sdf.format(date);
    }

    /**
     * 计算时间
     *
     * @param date
     * @param add_day    增加多少天
     * @param add_hour   增加多少小时
     * @param add_minute 增加多少分钟
     * @param add_second 增加多少秒
     * @return 计算后的时间
     */
    public static Date addDate(Date date, int add_day, int add_hour, int add_minute, int add_second) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(new Date());
        cal.add(Calendar.DAY_OF_MONTH, add_day);
        cal.add(Calendar.HOUR_OF_DAY, add_hour);
        cal.add(Calendar.MINUTE, add_minute);
        cal.add(Calendar.SECOND, add_second);
        return cal.getTime();
    }

    /**
     * 设置时间 如果年月日时分秒 参数为null，则不会设置该属性。例如：year 参数为空，则年用date中的年
     *
     * @param date
     * @param year   年
     * @param month  月
     * @param day    日
     * @param hour   时
     * @param minute 分
     * @param second 秒
     * @return
     */
    public static Date setDate(Date date, Integer year, Integer month, Integer day, Integer hour, Integer minute, Integer second) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        if (year != null) {
            cal.set(Calendar.YEAR, year);//年
        }
        if (month != null) {
            cal.set(Calendar.MONTH, (month));//月
        }
        if (day != null) {
            cal.set(Calendar.DAY_OF_MONTH, day);//日
        }
        if (hour != null) {
            cal.set(Calendar.HOUR_OF_DAY, hour);//时
        }
        if (minute != null) {
            cal.set(Calendar.MINUTE, minute);//分
        }
        if (second != null) {
            cal.set(Calendar.SECOND, second);//秒
        }
        return cal.getTime();
    }

    /**
     * 获得年
     * @param date
     * @return
     */
    public static int getYear(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        return cal.get(Calendar.YEAR);
    }

    /**
     * 获得月
     * @param date
     * @return
     */
    public static int getMonth(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        return cal.get(Calendar.MONTH);
    }

    /**
     * 获得日
     * @param date
     * @return
     */
    public static int getDay(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        return cal.get(Calendar.DAY_OF_MONTH);
    }

    /**
     * 获得时
     * @param date
     * @return
     */
    public static int getHour(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        return cal.get(Calendar.HOUR_OF_DAY);
    }

    /**
     * 获得分
     * @param date
     * @return
     */
    public static int getMinute(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        return cal.get(Calendar.MINUTE);
    }

    /**
     * 获得秒
     * @param date
     * @return
     */
    public static int getSecond(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        return cal.get(Calendar.SECOND);
    }


    public static void main(String[] args) {
        Date date = new Date();
        System.out.println(TimeUtil.formtDateTime(date));
        date = TimeUtil.addDate(date, 2, 2, 2, 2);
        System.out.println(TimeUtil.formtDateTime(date));
        date = TimeUtil.setDate(date, null, null, null, null, null, 2);
        System.out.println(TimeUtil.formtDateTime(date));
        System.out.println(TimeUtil.getYear(date));
        System.out.println(TimeUtil.getMonth(date));
        System.out.println(TimeUtil.getDay(date));
        System.out.println(TimeUtil.getHour(date));
        System.out.println(TimeUtil.getMinute(date));
        System.out.println(TimeUtil.getSecond(date));
    }
}
